File System (fs) Node
The fs module provides an API for interacting with the file system in a manner closely modeled around standard POSIX functions. code:file-system.js
const fs = require('fs');
処理終了後
引数: エラーとファイルの内容
例
code:async.js
//callback(引き渡される関数)の第1引数err,第2引数でデータ受け取り
fs.readFile(path, options, function(err, data) {
// ...
});
基本的に処理が止まったらまずい場合は非同期処理にすべき
code:sync.js
const data = fs.readFileSync(__dirname + '/test.txt', { encoding: 'utf8' });
種類
読み込み可能
書き込み可能
読み書き両方が可能
読み書きのタイミングでデータ加工が可能
カスタムストリーム